<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="30%" :title="`${drawerProps.title}用户信息`">
    <el-form
      label-width="100px"
      label-suffix=" :"
      :disabled="drawerProps.title === '查看'"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="设为管理员">
        <el-switch v-model="is_admin" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="handleSubmit"> 提交 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="editUserInfoDrawer">
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { getCourseTreeListApi } from "@/api/modules/course";
import TreeFilter from "@/components/TreeFilter/index.vue";
import { updateUserApi } from "@/api/modules/user";
const courseTreeFilter = ref<InstanceType<typeof TreeFilter>>();

const is_admin = ref<boolean>(false);
const set_admin_enable = ref<boolean>(false);
interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<any>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerProps = ref<any>({
  isView: false,
  title: "",
  row: {}
});

const drawerVisible = ref(false);

// 接收父组件传过来的参数
const acceptParams = (params: any) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  is_admin.value = params.row.role === "admin";
  set_admin_enable.value = params.row.user_id === "root";
};

// 提交数据（新增/编辑）
const handleSubmit = async () => {
  try {
    const params = {
      user_id: drawerProps.value.row.user_id,
      role: is_admin.value === true ? "admin" : ""
    };
    const data = await updateUserApi(params);
    if (data.code !== "") {
      ElMessage.error(data.msg);
      return;
    }
    ElMessage.success({ message: `提交成功！` });
    drawerProps.value.getTableList!();
    drawerVisible.value = false;
  } catch (error) {
    console.log(error);
  }
  return true;
};

defineExpose({
  acceptParams
});
</script>
<style scoped lang="scss">
@import "userEditor";
</style>
